package com.zzptc.sky.translate;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zzptc.sky.translate.ben.Login;

public class LoginActivity extends AppCompatActivity {
Toolbar toolbar_login;
    TextView textView_login;
    private ProgressDialog mDialog;
    private EditText name,password;
    private Button button_yes;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        toolbar_login= (Toolbar) findViewById(R.id.toolbar_login);
        name = (EditText) findViewById(R.id.login_name);
        password = (EditText) findViewById(R.id.login_password);
        textView_login = (TextView) findViewById(R.id.textView_login);
        setSupportActionBar(toolbar_login);
        //显示为已启用的setDisplayHomeAsUpEnabled
        //获得支持动作条 SupportActionBa
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mDialog = new ProgressDialog(this);
        mDialog.setMessage("正在登陆...");
        toolbar_login.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        button_yes= (Button) findViewById(R.id.button_yes);
        button_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpUtil.get("http://apicloud.mob.com/user/login?key=1f45339301beb&username="+name.getText().toString().trim()+"&password="+password.getText().toString().trim(),handler);
                mDialog.show();
            }
        });
        textView_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,RegistrationActivity.class));
            }
        });
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mDialog.cancel();
            System.out.println(msg.obj.toString()+"<------##################------->");
            Gson gson = new Gson();
            Login login = gson.fromJson(msg.obj.toString(),Login.class);
            if(login.getRetCode().trim().equals("200")){
                Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                finish();
            }
            switch (Integer.parseInt(login.getRetCode().trim())){
                case 22801: Toast.makeText(LoginActivity.this, "该用户未找到", Toast.LENGTH_SHORT).show(); break;
                case 22802: Toast.makeText(LoginActivity.this, "用户名不允许为空", Toast.LENGTH_SHORT).show(); break;
                case 22803: Toast.makeText(LoginActivity.this, "密码不允许为空", Toast.LENGTH_SHORT).show(); break;
                case 22807: Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show(); break;
            }
        }
    };
}
